<template>
  <div class="login">
    <div class="login-title">
      <p>登录</p>
    </div>

    <el-form :model="loginForm" :rules="rules" class="login-form">
      <el-form-item prop="username" class="form-item-username">
        <el-input placeholder="用户名" clearable v-model="loginForm.username">
          <template #prefix>
            <i class="el-icon-user"></i>
          </template>
        </el-input>
      </el-form-item>

      <el-form-item prop="password" class="form-item-password">
        <el-input placeholder="密码" show-password clearable v-model="loginForm.password"
                  @keyup.enter.native="login">

        </el-input>
      </el-form-item>

      <el-form-item class="form-item-remember-password">
        <el-checkbox v-model="rememberPassword">
          <span>记住密码</span>
        </el-checkbox>
        <span class="forgottenPassword" @click="forgetPassword">忘记密码?</span>
      </el-form-item>

      <el-form-item>
        <el-button class="button-login" @click="login" @keyup.enter.native="login">登录</el-button>
      </el-form-item>

      <p class="user-register">
        还没有注册帐号？
        <span @click="registerUser">立即注册</span>
      </p>

      <div class="login-by-other-ways">
        <p class="other-login-title">使用第三方登录</p>
      </div>

      <div class="other-login-wrap">
        <img src="../assets/QQ.png" @click="loginByQQ">
        <img src="../assets/wechat.png" @click="loginByWeChat">
      </div>
    </el-form>
  </div>
</template>

<script>
import { requestLogin } from '../network/api'

export default {
  name: 'LoginView',
  data () {
    return {
      rememberPassword: true,
      loginForm: {
        username: 'bobo',
        password: 'liyanjia'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在2到15个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在8到20个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login () {
      const loginFormData = new FormData()
      loginFormData.append('account', this.loginForm.username)
      loginFormData.append('password', this.loginForm.password)

      requestLogin(loginFormData).then(res => {
        if (res.data.code === '00000') {
          window.sessionStorage.setItem('authorization', res.headers.authorization)
          // this.$store.dispatch("setAuthorization", res.headers.authorization)b
          // this.$message.success(res.data.message)
          this.$router.push('/main')
        }
      })
      // window.sessionStorage.setItem("authorization", "token123")
      // this.$router.push("/main")
    },
    registerUser () {
      this.$router.push('/login/toRegister')
    },
    forgetPassword () {
      this.$message.success('忘记密码')
    },
    loginByQQ () {
      this.$message.success('QQ登录')
    },
    loginByWeChat () {
      this.$message.success('微信登录')
    }
  }
}
</script>

<style scoped>
.login {
  width: 50%;
  min-height: 500px;
  background: darksalmon;
  float: left;
  position: relative;
}

</style>
